package com.azusa.ktc_base.service.impl;

import com.azusa.ktc_base.dao.LabelDao;
import com.azusa.ktc_base.entity.Label;
import com.azusa.ktc_base.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import utils.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = {RuntimeException.class})
public class LabelServiceImpl implements LabelService {

    @Autowired
    LabelDao dao;


    @Override
    public List<Label> findAll() {
        return dao.findAll();
    }

    @Override
    public Page<Label> findPage(Integer page, Integer size){
        PageRequest request = PageRequest.of(page - 1, size);
        return dao.findAll(request);
    }

    @Override
    public Label save(Label label) {
        IdWorker worker = new IdWorker(1, 1);
        label.setId(worker.nextId() + "");
        return dao.save(label);
    }

    @Override
    public List<Label> findByRecommend(String recommend) {
        return dao.findByRecommend(recommend);
    }

    @Override
    public List<Label> findByState(String state) {
        return dao.findByState(state);
    }

    @Override
    public Label findById(String id) {
        return dao.findById(id).get();
    }

    @Override
    public void deleteById(String id) {
        dao.deleteById(id);
    }

    @Override
    public List<Label> findSearch(Label label) {
        Specification<Label> specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (label.getLabelname() != null && !StringUtils.isEmpty(label.getLabelname())){
                    predicateList.add(criteriaBuilder.like(root.get("labelname").as(String.class),label.getLabelname()));
                }
                if (label.getRecommend() != null && !StringUtils.isEmpty(label.getRecommend())){
                    predicateList.add(criteriaBuilder.like(root.get("recommend").as(String.class),label.getRecommend()));
                }
                if (label.getState() != null && !StringUtils.isEmpty(label.getState())){
                    predicateList.add(criteriaBuilder.like(root.get("state").as(String.class),label.getState()));
                }
                if (label.getCount() != null && label.getCount() >= 0){
                    predicateList.add(criteriaBuilder.equal(root.get("count").as(Long.class),label.getCount()));
                }
                if (label.getId() != null && !StringUtils.isEmpty(label.getId())){
                    predicateList.add(criteriaBuilder.equal(root.get("id").as(Long.class),label.getId()));
                }
                return criteriaQuery.where(criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]))).getRestriction();
            }
        };
        return dao.findAll(specification);
    }
}
